package org.ponly.cta4j.factory.gimpy;

import java.awt.image.BufferedImage;

import org.ponly.cta4j.factory.AbstractCaptcha;

/**
 * Gimpy 验证码.
 *
 * @author vacoor
 * @since 1.0
 */
public class Gimpy extends AbstractCaptcha {
    /**
     * Response.
     */
    private final String response;

    /**
     * 区分大小写.
     */
    private final boolean caseSensitive;

    /**
     * 构建一个大小写不敏感的验证码.
     *
     * @param challenge Challenge
     * @param response  Response
     */
    Gimpy(final BufferedImage challenge, final String response) {
        this(challenge, response, false);
    }

    /**
     * 使用给定配置创建一个验证码.
     *
     * @param challenge     验证码图片
     * @param response      response
     * @param caseSensitive 是否大小写敏感
     */
    Gimpy(final BufferedImage challenge, final String response, final boolean caseSensitive) {
        super(challenge);
        this.response = response;
        this.caseSensitive = caseSensitive;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean validate(final Object response) {
        final String input = null != response ? String.valueOf(response) : null;
        final String expect = getResponse();
        return isCaseSensitive() ? expect.equals(input) : expect.equalsIgnoreCase(input);
    }

    /**
     * @return Response
     */
    public String getResponse() {
        return response;
    }

    /**
     * @return 是否区分大小写
     */
    public boolean isCaseSensitive() {
        return caseSensitive;
    }
}
